const express = require("express");
const path = require("path");
const fs = require("fs").promises;

const categoryFoodData = express.Router();
let dataRes = [];

const dataFilePath = path.join(__dirname, "./CategoryFood.json");
async function query() {
    const fileContent = await fs.readFile(dataFilePath, "utf8");
    dataRes = JSON.parse(fileContent);
    return dataRes;
}

categoryFoodData.get("/", async (req, res) => {
    try {
        const data = await query();
        res.send({
        code: 200, // 0 表示处理成功，1 表示处理失败
        message: "get 请求成功！", // 状态的描述
        result: data // 需要响应给客户端的数据
    });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});
module.exports = categoryFoodData;
